package cn.hh.pet.service.impl;


import cn.hh.basic.service.impl.BasicServiceImpl;
import cn.hh.basic.util.*;
import cn.hh.order.domain.PetAcquisitionOrder;
import cn.hh.order.mapper.PetAcquisitionOrderMapper;
import cn.hh.org.domain.Employee;
import cn.hh.org.domain.Shop;
import cn.hh.org.mapper.EmpMapper;
import cn.hh.org.mapper.ShopMapper;
import cn.hh.org.service.IShopService;
import cn.hh.pet.domain.Pet;
import cn.hh.pet.domain.PetDetail;
import cn.hh.pet.domain.SMM;
import cn.hh.pet.mapper.PetDetailMapper;
import cn.hh.pet.mapper.PetMapper;
import cn.hh.pet.mapper.SMMMapper;
import cn.hh.pet.query.SMMQuery;
import cn.hh.pet.service.IPetService;
import cn.hh.pet.service.ISMMService;
import cn.hh.user.domain.User;
import cn.hh.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SMMServiceImpl extends BasicServiceImpl<SMM> implements ISMMService {
    @Autowired
    private IShopService ShopService;

    @Autowired
    private SMMMapper smmMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private IPetService petService;
    @Autowired
    private PetAcquisitionOrderMapper acquisitionOrderMapper;
    /*发布寻主信息*/
    @Override
    @Transactional
    public void publish(SMM smm, Long LoginId) throws Exception {
        // 1.寻找最近店铺
        /*使用工具类传入前台的地址和所有店铺地址信息，计算后返回最近店铺*/
        /*传入前台地址*/
        Point point = DistanceUtil.getPoint(smm.getAddress());
        /*获取所有店铺*/
        List<Shop> shops = shopMapper.loadAll();
        /*实用工具类获取最近店铺*/
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        System.out.println("最近店铺ID"+shop.getId());
        // 2.绑定最近店铺
        smm.setShop_id(shop.getId());
        // 3.绑定用户消息
        /*获取该用户信息*/
        User user = userMapper.loadByLoginId(LoginId);
        smm.setUser_id(user.getId());
        // 4.储存寻主消息
        smmMapper.add(smm);
        // 5.提示店铺老板有新业务
        /*获取店铺管理员的电话或邮件信息*/
        Employee employee = empMapper.loadById(shop.getAdmin_id());
        /*发送短信或者邮件*/
        /*SMSUtils.send(employee.getPhone(),"HXD，来"+smm.getTitle()+"活儿了，快出来接客"  );*/
        System.out.println("HXD，来" + smm.getTitle() + "活儿了，快出来接客");
    }
    /*待处理消息查看*/
    @Override
    public PageList<SMM> pending(SMMQuery query, Long loginId) {
        /*设置查询的状态*/
        query.setState(0);
        /*查询登录的信息*/
        Employee employee = empMapper.loadByLoginId(loginId);
        /*如果是店铺员工*/
        if(employee.getShop_id()!=null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }
    /*已处理消息查看*/
    @Override
    public PageList<SMM> processed(SMMQuery query, Long loginId) {
        /*设置查询的状态*/
        query.setState(1);
        /*查询登录的信息*/
        Employee employee = empMapper.loadByLoginId(loginId);
        /*如果是店铺员工*/
        if(employee.getShop_id()!=null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }
    /*用户消息*/
    @Override
    public PageList<SMM> list(SMMQuery query, Long loginId) {
        /*查询登录的信息*/
        User user = userMapper.loadByLoginId(loginId);
        /*能查到是员工*/
        if (user.getId()!=null) {
            query.setUserId(user.getId());
        }
        return super.queryPage(query);

    }
    /*处理消息*/
    @Override
    public void handle(Pet pet,Long loginId) {
        /*通过发布信息id更改发布信息状态*/
        System.out.println("更改的id"+pet.getSearch_master_msg_id());
        smmMapper.updateState(pet.getSearch_master_msg_id());
        /*避免代码冗余，转到pet处理*/
        petService.add(pet);
        Employee employee = empMapper.loadByLoginId(loginId);
        SMM smm = smmMapper.loadById(pet.getSearch_master_msg_id());
        /*smm转为order*/
        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, smm, employee.getId());
        /*保存订单*/
        acquisitionOrderMapper.add(petAcquisitionOrder);
        System.out.println("支付成功"+pet.getCostprice());
    }
    private PetAcquisitionOrder pet2order(Pet pet, SMM smm, Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(smm.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(smm.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(smm.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }
}
